package com.rpay.web.controller.index;

import com.rpay.common.dto.user.UserRippleDTO;
import com.rpay.common.util.JsoupUtil;
import com.rpay.common.vo.PcsResult;
import com.rpay.model.user.UserRippleDO;
import com.rpay.service.reptile.IReptileService;
import com.rpay.service.user.IUserIncomeService;
import com.rpay.web.interceptor.authentic.AccessControl;
import com.rpay.web.interceptor.authentic.AccessControlType;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * Title：
 * Description
 *
 * @author fxg06 on 2018/5/6
 * @version 1.0
 */
@RestController
@RequestMapping(value = "api/index")
@AccessControl(type = AccessControlType.SECRET)
@Validated
public class IndexController {
    @Autowired
    private IReptileService iReptileService;

    @Autowired
    private IUserIncomeService iUserIncomeService;

    @GetMapping(value = "")
    public PcsResult index() {
        JSONArray jsonArray = JsoupUtil.reptileByFeiXiaoHao();
        if (jsonArray != null) {
            iReptileService.saveReptile("FEIXIAOHAO", "https://www.feixiaohao.com/coinmarket/ripple/", jsonArray.toString());
            return PcsResult.build(jsonArray.toString());
        } else {
            String content = iReptileService.getReptile("FEIXIAOHAO");
            return PcsResult.build(content);
        }
    }

    @GetMapping(value = "test")
    public PcsResult test() {
        UserRippleDTO userRippleDTO = new UserRippleDTO();
        userRippleDTO.setUserId(13);
        userRippleDTO.setPrice(new BigDecimal(5));
        userRippleDTO.setInvestAmount(new BigDecimal(10000));
        iUserIncomeService.updateUserAchievement(userRippleDTO);
        return new PcsResult();
    }
}
